﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using System.Xml;
using YArchitech.LIB;
using YArchitech.MEP.Electric.Common;
using HYElectricCommon;

namespace HYRElectric.LightningCalculation
{
	public partial class ThunderstormDaysSettingForm : System.Windows.Forms.Form
	{
		public ThunderstormDaysSettingForm()
		{
			this.InitializeComponent();
			this.btnAdd.Image = Resource.GetImage("AddNew16");
			this.btnEdit.Image = Resource.GetImage("modify16");
			this.btnDelete.Image = Resource.GetImage("delete16");
		}

		public string GetThunderstormDays
		{
			get
			{
				string result;
				try
				{
					result = this.ProvinceListView.SelectedItems[0].SubItems[2].Text;
				}
				catch (Exception)
				{
					result = "";
				}
				return result;
			}
		}

		private void ThunderstormDaysSettingForm_Load(object sender, EventArgs e)
		{
			//base.Icon = Resource.YArchitechIcon;
			ToolTip toolTip = new ToolTip();
			toolTip.InitialDelay = 500;
			toolTip.AutoPopDelay = 3000;
			toolTip.ReshowDelay = 50;
			toolTip.ShowAlways = true;
			toolTip.SetToolTip(this.btnAdd, "添加");
			toolTip.SetToolTip(this.btnEdit, "编辑");
			toolTip.SetToolTip(this.btnDelete, "删除");
			string filePath = this.SettingFileFillPath();
			if (this.ReadThunderstormDaysInfos(filePath, ref this.m_ThunderstormDaysInfosAry))
			{
				foreach (ThunderstormDaysSettingForm.ThunderstormDaysInfos thunderstormDaysInfos in this.m_ThunderstormDaysInfosAry)
				{
					this.cmbProvince.Items.Add(thunderstormDaysInfos.Province);
				}
				if (this.m_ThunderstormDaysInfosAry.Count > 0)
				{
					this.cmbProvince.SelectedIndex = 0;
					this.UpdateCurrentSettingInfos(this.m_ThunderstormDaysInfosAry[0]);
				}
			}
			this.LoadDlgData();
		}

		private void ThunderstormDaysSettingForm_FormClosing(object sender, FormClosingEventArgs e)
		{
			if (base.DialogResult != DialogResult.OK)
			{
				return;
			}
			string filePath = this.SettingFileFillPath();
			this.WriteThunderstormDaysInfos(filePath, this.m_ThunderstormDaysInfosAry);
			this.SaveDlgData();
		}

		private void SaveDlgData()
		{
			string workingLocation = Product.WorkingLocation;
			string xmlPath = Path.Combine(workingLocation, "ThunderstormDaysSettingForm.xml");
			YArchitech.MEP.Electric.Common.XmlHelper xmlHelper = new YArchitech.MEP.Electric.Common.XmlHelper(workingLocation, "Root");
			xmlHelper.SelectNode("Root\\Datas");
			xmlHelper.SetValue("X", base.Location.X.ToString());
			xmlHelper.SetValue("Y", base.Location.Y.ToString());
			xmlHelper.SetValue("SelectedIndex", this.cmbProvince.SelectedIndex.ToString());
			xmlHelper.SaveXml(xmlPath);
		}

		private void LoadDlgData()
		{
			string text = Path.Combine(Product.WorkingLocation, "ThunderstormDaysSettingForm.xml");
			if (!File.Exists(text))
			{
				return;
			}
			YArchitech.MEP.Electric.Common.XmlHelper xmlHelper = new YArchitech.MEP.Electric.Common.XmlHelper(text, "Root");
			xmlHelper.SelectNode("Root\\Datas");
			int x = Convert.ToInt32(xmlHelper.GetValue("X"));
			int y = Convert.ToInt32(xmlHelper.GetValue("Y"));
			base.Location = new System.Drawing.Point(x, y);
			int num = Convert.ToInt32(xmlHelper.GetValue("SelectedIndex"));
			if (num < this.cmbProvince.Items.Count)
			{
				this.cmbProvince.SelectedIndex = num;
			}
		}

		private void btnAdd_Click(object sender, EventArgs e)
		{
			List<string> list = new List<string>();
			foreach (object obj in this.ProvinceListView.Items)
			{
				string text = ((ListViewItem)obj).SubItems[1].Text;
				list.Add(text);
			}
			AddEditCityThunderstormDaysForm addEditCityThunderstormDaysForm = new AddEditCityThunderstormDaysForm("", 0.0, list);
			if (addEditCityThunderstormDaysForm.ShowDialog(this) != DialogResult.OK)
			{
				return;
			}
			this.ProvinceListView.BeginUpdate();
			ListViewItem listViewItem = new ListViewItem();
			listViewItem.Text = "";
			listViewItem.SubItems.Add(addEditCityThunderstormDaysForm.NewCityName);
			listViewItem.SubItems.Add(addEditCityThunderstormDaysForm.NewCount.ToString());
			this.ProvinceListView.Items.Add(listViewItem);
			this.ProvinceListView.EndUpdate();
			this.m_ThunderstormDaysInfosAry[this.cmbProvince.SelectedIndex].Citys.Add(addEditCityThunderstormDaysForm.NewCityName);
			this.m_ThunderstormDaysInfosAry[this.cmbProvince.SelectedIndex].Days.Add(addEditCityThunderstormDaysForm.NewCount);
		}

		private void btnEdit_Click(object sender, EventArgs e)
		{
			if (this.ProvinceListView.SelectedItems.Count != 1)
			{
				return;
			}
			List<string> list = new List<string>();
			foreach (object obj in this.ProvinceListView.Items)
			{
				string text = ((ListViewItem)obj).SubItems[1].Text;
				list.Add(text);
			}
			string text2 = this.ProvinceListView.SelectedItems[0].SubItems[1].Text;
			double currentCount = Convert.ToDouble(this.ProvinceListView.SelectedItems[0].SubItems[2].Text);
			AddEditCityThunderstormDaysForm addEditCityThunderstormDaysForm = new AddEditCityThunderstormDaysForm(text2, currentCount, list);
			if (addEditCityThunderstormDaysForm.ShowDialog(this) != DialogResult.OK)
			{
				return;
			}
			this.ProvinceListView.BeginUpdate();
			this.ProvinceListView.SelectedItems[0].SubItems[1].Text = addEditCityThunderstormDaysForm.NewCityName;
			this.ProvinceListView.SelectedItems[0].SubItems[2].Text = addEditCityThunderstormDaysForm.NewCount.ToString();
			this.ProvinceListView.EndUpdate();
			int index = this.ProvinceListView.SelectedItems[0].Index;
			this.m_ThunderstormDaysInfosAry[this.cmbProvince.SelectedIndex].Citys[index] = addEditCityThunderstormDaysForm.NewCityName;
			this.m_ThunderstormDaysInfosAry[this.cmbProvince.SelectedIndex].Days[index] = addEditCityThunderstormDaysForm.NewCount;
		}

		private void btnDelete_Click(object sender, EventArgs e)
		{
			if (this.ProvinceListView.Items.Count == 1)
			{
				MessageBox.Show("不能删除最后一条记录！", "盈建科提示");
				return;
			}
			if (this.ProvinceListView.SelectedItems.Count == 1)
			{
				int index = this.ProvinceListView.SelectedItems[0].Index;
				this.ProvinceListView.BeginUpdate();
				this.ProvinceListView.Items.RemoveAt(index);
				this.ProvinceListView.EndUpdate();
				this.m_ThunderstormDaysInfosAry[this.cmbProvince.SelectedIndex].Citys.RemoveAt(index);
				this.m_ThunderstormDaysInfosAry[this.cmbProvince.SelectedIndex].Days.RemoveAt(index);
			}
		}

		private ThunderstormDaysSettingForm.ThunderstormDaysInfos GetCurrentSettingInfos()
		{
			string text = this.cmbProvince.Text;
			List<string> list = new List<string>();
			List<double> list2 = new List<double>();
			foreach (object obj in this.ProvinceListView.Items)
			{
				ListViewItem listViewItem = (ListViewItem)obj;
				string text2 = listViewItem.SubItems[1].Text;
				list.Add(text2);
				double item = Convert.ToDouble(listViewItem.SubItems[1].Text);
				list2.Add(item);
			}
			return new ThunderstormDaysSettingForm.ThunderstormDaysInfos(text, list, list2);
		}

		private void UpdateCurrentSettingInfos(ThunderstormDaysSettingForm.ThunderstormDaysInfos thunderstormDaysInfos)
		{
			this.ProvinceListView.BeginUpdate();
			this.ProvinceListView.Items.Clear();
			int i = 0;
			int count = thunderstormDaysInfos.Citys.Count;
			while (i < count)
			{
				ListViewItem listViewItem = new ListViewItem();
				listViewItem.Text = "";
				listViewItem.SubItems.Add(thunderstormDaysInfos.Citys[i]);
				listViewItem.SubItems.Add(thunderstormDaysInfos.Days[i].ToString());
				this.ProvinceListView.Items.Add(listViewItem);
				i++;
			}
			if (this.ProvinceListView.Items.Count > 0)
			{
				this.ProvinceListView.Items[0].Selected = true;
			}
			this.ProvinceListView.EndUpdate();
		}

		private bool ReadThunderstormDaysInfos(string filePath, ref List<ThunderstormDaysSettingForm.ThunderstormDaysInfos> thunderstormDaysInfosAry)
		{
			XmlDocument xmlDocument = new XmlDocument();
			try
			{
				xmlDocument.Load(filePath);
			}
			catch (Exception)
			{
				return false;
			}
			XmlNode xmlNode = xmlDocument.SelectSingleNode("Provinces");
			if (xmlNode == null)
			{
				return false;
			}
			foreach (object obj in xmlNode.ChildNodes)
			{
				XmlElement xmlElement = ((XmlNode)obj) as XmlElement;
				string attribute = xmlElement.GetAttribute("Name");
				List<string> list = new List<string>();
				List<double> list2 = new List<double>();
				foreach (object obj2 in xmlElement.ChildNodes)
				{
					XmlElement xmlElement2 = ((XmlNode)obj2) as XmlElement;
					string attribute2 = xmlElement2.GetAttribute("Name");
					list.Add(attribute2);
					try
					{
						double item = Convert.ToDouble(xmlElement2.GetAttribute("Day"));
						list2.Add(item);
					}
					catch (Exception)
					{
						list2.Add(0.0);
					}
				}
				ThunderstormDaysSettingForm.ThunderstormDaysInfos item2 = new ThunderstormDaysSettingForm.ThunderstormDaysInfos(attribute, list, list2);
				thunderstormDaysInfosAry.Add(item2);
			}
			return true;
		}

		private void WriteThunderstormDaysInfos(string filePath, List<ThunderstormDaysSettingForm.ThunderstormDaysInfos> thunderstormDaysInfosAry)
		{
			XmlDocument xmlDocument = new XmlDocument();
			XmlNode xmlNode = xmlDocument.CreateNode(XmlNodeType.XmlDeclaration, "", "");
			XmlNode xmlNode2 = xmlNode;
			xmlNode2.InnerText += " encoding=\"GB2312\"";
			xmlDocument.AppendChild(xmlNode);
			XmlElement xmlElement = xmlDocument.CreateElement("", "Provinces", "");
			xmlDocument.AppendChild(xmlElement);
			foreach (ThunderstormDaysSettingForm.ThunderstormDaysInfos thunderstormDaysInfos in thunderstormDaysInfosAry)
			{
				XmlElement xmlElement2 = xmlDocument.CreateElement("Province");
				xmlElement2.SetAttribute("Name", thunderstormDaysInfos.Province);
				int i = 0;
				int count = thunderstormDaysInfos.Citys.Count;
				while (i < count)
				{
					XmlElement xmlElement3 = xmlDocument.CreateElement("City");
					xmlElement3.SetAttribute("Name", thunderstormDaysInfos.Citys[i]);
					xmlElement3.SetAttribute("Day", thunderstormDaysInfos.Days[i].ToString());
					xmlElement2.AppendChild(xmlElement3);
					i++;
				}
				xmlElement.AppendChild(xmlElement2);
			}
			xmlDocument.Save(filePath);
		}

		private string SettingFileFillPath()
		{
			string text = Product.WorkingLocation + "LightningCalculation.xml";
			if (!File.Exists(text))
			{
				string sourceFileName = AssistFunc.GetAppRootPath() + "\\Data\\ElectricalData\\LightningCalculation.xml";
				try
				{
					File.Copy(sourceFileName, text);
				}
				catch (Exception)
				{
				}
			}
			return text;
		}

		private void cmbProvince_SelectedIndexChanged(object sender, EventArgs e)
		{
			this.UpdateCurrentSettingInfos(this.m_ThunderstormDaysInfosAry[this.cmbProvince.SelectedIndex]);
		}

		private void ProvinceListView_MouseUp(object sender, MouseEventArgs e)
		{
			if (this.ProvinceListView.SelectedItems.Count == 0 && this.ProvinceListView.FocusedItem != null && this.ProvinceListView.GetItemAt(e.X, e.Y) == null)
			{
				this.ProvinceListView.FocusedItem.Selected = true;
			}
		}

		private List<ThunderstormDaysSettingForm.ThunderstormDaysInfos> m_ThunderstormDaysInfosAry = new List<ThunderstormDaysSettingForm.ThunderstormDaysInfos>();

		private class ThunderstormDaysInfos
		{
			public string Province { get; set; }

			public List<string> Citys { get; set; }

			public List<double> Days { get; set; }

			private ThunderstormDaysInfos()
			{
			}

			public ThunderstormDaysInfos(ThunderstormDaysSettingForm.ThunderstormDaysInfos infos)
			{
				this.Province = infos.Province;
				this.Citys = infos.Citys;
				this.Days = infos.Days;
			}

			public ThunderstormDaysInfos(string province, List<string> citys, List<double> days)
			{
				this.Province = province;
				this.Citys = citys;
				this.Days = days;
			}
		}
	}
}
